package com.xm.xmds1705083.service.service.impl;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.xm.xm.dto.ResultDto;
import com.xm.xm.dto.SearchDto;
import com.xm.xm.util.ErrorCode;
import com.xm.xm.util.SmsUtils;
import com.xm.xm.util.ValidateUtils;
import com.xm.xmds1705083.service.config.MsgConfig;
import com.xm.xmds1705083.service.mapper.DuoduoSmsMapper;
import com.xm.xmds1705083.service.model.DuoduoSms;
import com.xm.xmds1705083.service.service.DuoduoSmsService;
import com.xm.xmds1705083.service.util.HttpSender;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Random;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 杭州新苗网络科技有限公司
 * @author meihf
 * @date 2017/06/15
 * @description
*/ 
@Service("duoduoSmsService")
public class DuoduoSmsServiceImpl extends ServiceImpl<DuoduoSmsMapper,DuoduoSms> implements DuoduoSmsService {

    @Autowired
    DuoduoSmsMapper duoduoSmsMapper;

    @Override
    public ResultDto sendMessage(SearchDto searchDto) {
        //1是注册 2是找回密码 3是预约 4是添加我的车辆
        ReentrantLock reentrantLock = new ReentrantLock();
        reentrantLock.lock();
        ResultDto resultDto = new ResultDto();
        JSONObject filed = searchDto.getFiled();
        searchDto.setPageNo((searchDto.getPageNo()-1)*searchDto.getPageSize());
        try {
             if (!filed.has("phone") || !filed.has("sendtype")){
                  resultDto.setMsg(ErrorCode.E10011);
                  resultDto.setCode(ErrorCode.C10011);
                  return resultDto;
              }else{
                 String phone = filed.getString("phone");
                 //sendtype1是注册2是找回密码
                 int sendtype = filed.getInt("sendtype");
                 if(!ValidateUtils.isMobileNO(Long.parseLong(phone))){
                     resultDto.setCode(ErrorCode.C10004);
                     resultDto.setMsg(ErrorCode.E10004);
                     return  resultDto;
                 }

                 Random random = new Random();
                 Integer validateCode = random.nextInt(899999);
                 validateCode = validateCode + 100000;

                 String content = MsgConfig.PRODUCT;
                 //添加发送短信内容
                 if (sendtype == 2){
                     content += "您的验证码是:"+validateCode+"，您正在通过手机号重置登录密码，10分钟内有效,如非本人操作，请忽略该短信。";
                 }else {
                     content += "您的验证码是:"+validateCode+"，10分钟内有效。请不要把验证码泄漏给其他人，如非本人请勿操作。";
                 }
                 String uri = MsgConfig.URL;//应用地址
                 String account = MsgConfig.ACCOUNT;//账号
                 String pswd = MsgConfig.PASSWORD;//密码
                 String mobiles = phone;//手机号码，多个号码使用","分割
                 boolean needstatus = true;//是否需要状态报告，需要true，不需要false
                 String productId = MsgConfig.PRODUCTId;//产品ID(不用填写)
                 String extno = MsgConfig.EXTNO;//扩展码(请登陆网站用户中心——>服务管理找到签名对应的extno并填写，线下用户请为空)
                 ResultDto result = HttpSender.batchSend(uri, account, pswd, mobiles, content, needstatus, productId, extno);
                 if (result.getCode()>0){
                     return result;
                 }
                 //保存到表里面
                 DuoduoSms duoduoSms = new DuoduoSms();
                 duoduoSms.setContent(content);
                 duoduoSms.setCheckcode(validateCode);
                 duoduoSms.setCreateTime(searchDto.getTime().intValue());
                 duoduoSms.setSendtype(sendtype);
                 duoduoSms.setMobile(String.valueOf(phone));
                 duoduoSms.setValidity(searchDto.getTime().intValue()+ SmsUtils.tenMinute().intValue());
                 duoduoSmsMapper.insert(duoduoSms);
                 resultDto.setMsg("短信发送成功");
              }
        }catch (Exception e ){
            e.printStackTrace();
            resultDto.setCode(ErrorCode.C10013);
            resultDto.setMsg(ErrorCode.E10013);
        }finally {
            reentrantLock.unlock();
        }
        return resultDto;
    }

}

